package com.gnerv.cms.controller;


import com.gnerv.cms.common.Result;
import com.gnerv.cms.dto.TaskDTO;
import com.gnerv.cms.dto.TaskSaveDTO;
import com.gnerv.cms.dto.TaskSearchDTO;
import com.gnerv.cms.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 基础平台-任务表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Api(tags = "任务管理")
@CrossOrigin
@RestController
@RequestMapping("/api/manage/task")
public class TaskController {

    @Resource
    ITaskService taskService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveTask(@RequestBody TaskSaveDTO taskSaveDTO) {
        taskService.saveTask(taskSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "/delete/{taskId}", method = RequestMethod.GET)
    public Result deleteTask(@PathVariable(value = "taskId") String taskId) {
        taskService.deleteTask(taskId);
        return Result.response();
    }

    @RequestMapping(value = "/select/{taskId}", method = RequestMethod.GET)
    public Result selectTask(@PathVariable(value = "taskId") String taskId) {
        TaskDTO task = taskService.selectTask(taskId);
        return Result.response().data("task", task);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listTask(TaskSearchDTO taskSearchDTO){
        List<TaskDTO> tasks = taskService.listTask(taskSearchDTO);
        return Result.response().data("tasks", tasks);
    }

    @RequestMapping(value = "/start/{taskId}", method = RequestMethod.GET)
    public Result startTask(@PathVariable(value = "taskId") String taskId) {
        taskService.startTask(taskId);
        return Result.response();
    }

    @RequestMapping(value = "/stop/{taskId}", method = RequestMethod.GET)
    public Result stopTask(@PathVariable(value = "taskId") String taskId) {
        taskService.stopTask(taskId);
        return Result.response();
    }

}
